﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class DearlersSearch : System.Web.UI.Page
    {
        private string name;
        private string state;
        private string owner;
        private string sqlquery;
        private int dealer_id;
        private int counter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.IsInRole("Manager"))
            {
                HLaddDeal.Visible = false;
                HLaddDeal.Enabled = false;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            name = txtname.Text;
            if (ddlState.SelectedIndex > 0)
                state = ddlState.SelectedValue;
            else 
                state = string.Empty;
            owner = txtowner.Text;

            counter = 0;
            sqlquery = "Select * from Dealerships";

            if (name != string.Empty)
            {
                counter++;
                if (counter > 1)
                    sqlquery += " and Name like '%" + name + "%'";
                else
                    sqlquery += " where Name like '%" + name + "%'";
            }
            if (state != string.Empty)
            {
                counter++;
                if (counter > 1)
                    sqlquery += " and State like  '%" + state + "%'";
                else
                    sqlquery += " where State like '%" + state + "%'";
            }

            if (owner != string.Empty)
            {
                counter++;
                if (counter > 1)
                    sqlquery += " and owner like '%" + owner + "%'";
                else
                    sqlquery += " where owner like '%" + owner + "%'";
            }
            try
            {
                srcDealers.SelectCommand = sqlquery;
                ((Main)((SearchMP)this.Master).Master).clearStatus();
                grdDealers.DataBind();
            }
            catch (Exception ex)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
            }
        }

       /* protected void grdDealers_OnRowCreated(Object sender, GridViewRowEventArgs e)
        {
            if (!User.IsInRole("Manager"))
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[5].CssClass = "hiddencol";
                }
                else if (e.Row.RowType == DataControlRowType.Header)
                {
                    e.Row.Cells[5].CssClass = "hiddencol";
                }
            }
        }*/
    }
}
